package com.example.steveaoblogdemo.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.steveaoblogdemo.entity.Articles;
import com.example.steveaoblogdemo.entity.User;
import com.example.steveaoblogdemo.mapper.ArticlesMapper;
import com.example.steveaoblogdemo.mapper.UserMapper;
import com.example.steveaoblogdemo.service.ArticlesService;
import com.example.steveaoblogdemo.utils.RedisUtil;
import com.example.steveaoblogdemo.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class ArticlesServiceImpl extends ServiceImpl<ArticlesMapper, Articles> implements ArticlesService {
    @Resource
    ArticlesMapper articlesMapper;

    @Resource
    UserMapper userMapper;

    @Resource
    RedisUtil redisUtil;
    //博客首页查询
    public Result<?> queryBlog(@RequestParam Integer pageNum, @RequestParam Integer pageSize,@RequestParam String search){
        QueryWrapper<Articles> queryWrapper = new QueryWrapper();
        if (StrUtil.isNotBlank(search)){
            queryWrapper.eq("status",0).like("title",search);
        }
        queryWrapper.eq("status",0);
        IPage<Articles> articlesIPage = articlesMapper.selectPage(new Page<>(pageNum,pageSize),queryWrapper);
            return new Result<>(200,"查询成功",articlesIPage);
    }
    //新增博客
    public int addBlog(@RequestParam Integer id, @RequestParam String author,@RequestParam String title,
                       @RequestParam String summarize,@RequestParam String content ,@RequestParam String tags,
                       @RequestParam String category, @RequestParam String createTime){
        articlesMapper.addBlog(id,author,title,summarize,content,tags,category,createTime);
        Articles articles = new Articles();
        articles.setId(id);
        articles.setTitle(title);
        articles.setAuthor(author);
        articles.setSummarize(summarize);
        articles.setTags(tags);
        articles.setCategory(category);
        articles.setContent(content);
        return 0;
    }
    //根据ID获取博客内容
    public Result<?> queryById(@RequestParam Integer id){
        QueryWrapper<Articles> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        Articles articles = articlesMapper.selectOne(wrapper);
        if (articles!=null){
            return new Result<>(200,"查询成功",articles);
        }

        return new Result<>(400,"查询失败", null);
    }

    //更新博客
    public int updateBlog(@RequestParam String author, @RequestParam String title, @RequestParam String summarize,
                          @RequestParam String content , @RequestParam String tags, @RequestParam String category,
                          @RequestParam String createTime, @RequestParam Integer id){
        articlesMapper.updateBlog(author,title,summarize,content,tags,category,createTime,id);
        return 0;
    }

   //逻辑删除博客
   public Result<?> deleteById(@RequestParam Integer id){
       UpdateWrapper<Articles> wrapper = new UpdateWrapper();
       wrapper.eq("id",id).set("status",1);
       int result = articlesMapper.update(null,wrapper);
       return new Result<>(200,"删除成功",result);
   }

   //点赞博客
    public Result<?> likeBlog(Integer id,Integer uid){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("id",uid);
        String key = "BlogID:"+id.toString();
        User user = userMapper.selectOne(wrapper);
        if (user.getRecommend() == 0){
            redisUtil.SetS(key,uid.toString());
            Double like_num = redisUtil.zsIncr("likeNum",String.valueOf(id),1);
            int x = like_num.intValue();
            UpdateWrapper<Articles> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id",id).set("recommend_num",x);
            int result = articlesMapper.update(null,updateWrapper);
            System.out.println(result);
            UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
            userUpdateWrapper.eq("id",uid).set("recommend",1);
            int result1 = userMapper.update(null,userUpdateWrapper);
            System.out.println(result1);
            return new Result<>(200,"点赞成功",result1);
        }
        //判断该用户是否存在对应的点赞记录，key是博客的ID,value是用户的ID
        Boolean isLiked = redisUtil.hasSet(key,uid.toString());
        if (isLiked){
            Double like_num = redisUtil.zsDecr("likeNum",String.valueOf(id),1);
            int y = like_num.intValue();
            System.out.println(y);
            if (y>=0){
                UpdateWrapper<Articles> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("id",id).set("recommend_num",y);
                int result = articlesMapper.update(null,updateWrapper);
                System.out.println(result);
                UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
                userUpdateWrapper.eq("id",uid).set("recommend",0);
                int result1 = userMapper.update(null,userUpdateWrapper);
                System.out.println(result1);
                return new Result<>(400,"点赞失败",result);
            }
            return new Result<>(401,"点赞数为负",null);
        }
        redisUtil.SetS(key,uid.toString());
        Double like_num = redisUtil.zsIncr("likeNum",String.valueOf(id),1);
        int x = like_num.intValue();
        UpdateWrapper<Articles> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id).set("recommend_num",x);
        int result = articlesMapper.update(null,updateWrapper);
        System.out.println(result);
        return new Result<>(200,"点赞成功",result);
    }
}
